<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--类别数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="typeName"
            placeholder="请输类别名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="typeOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--案卷信息数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" v-show="showSearch" label-width="68px">
          <el-row>
            <el-col :span="6">
              <el-form-item label="档案号" prop="archiveNo">
                <el-input
                  v-model="queryParams.archiveNo"
                  placeholder="请输入档案号"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="题名" prop="archiveName">
                <el-input
                  v-model="queryParams.archiveName"
                  placeholder="请输入题名"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="副题名" prop="subName">
                <el-input
                  v-model="queryParams.subName"
                  placeholder="请输入副题名"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="责任者" prop="dutyName">
                <el-input
                  v-model="queryParams.dutyName"
                  placeholder="请输入责任者"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="保存年限" prop="limitYear">
                <el-input
                  v-model="queryParams.limitYear"
                  placeholder="请输入保存年限"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="所在盒号" prop="boxNo">
                <el-input
                  v-model="queryParams.boxNo"
                  placeholder="请输入所在盒号"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="所在柜号" prop="cupboardNo">
                <el-input
                  v-model="queryParams.cupboardNo"
                  placeholder="请输入所在柜号"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:archive:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['system:archive:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:archive:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['system:archive:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="archiveList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="档案号" align="center" prop="archiveNo" sortable />
          <el-table-column label="类别" align="center" prop="recordType.typeName" sortable />
          <el-table-column label="题名" align="center" prop="archiveName" sortable />
          <el-table-column label="副题名" align="center" prop="subName" sortable />
          <el-table-column label="责任者" align="center" prop="dutyName" sortable />
          <el-table-column label="保存年限" align="center" prop="limitYear" sortable />
          <el-table-column label="所在盒号" align="center" prop="boxNo" sortable />
          <el-table-column label="所在柜号" align="center" prop="cupboardNo" sortable />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:archive:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:archive:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改案卷信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="档案号" prop="archiveNo">
              <el-input v-model="form.archiveNo" placeholder="请输入档案号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="类别" prop="typeId">
              <treeselect
                v-model="form.typeId"
                :options="typeOptions"
                :show-count="true"
                placeholder="请选择"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="题名" prop="archiveName">
              <el-input v-model="form.archiveName" placeholder="请输入题名" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="副题名" prop="subName">
              <el-input v-model="form.subName" placeholder="请输入副题名" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="责任者" prop="dutyName">
              <el-input v-model="form.dutyName" placeholder="请输入责任者" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="保存年限" prop="limitYear">
              <el-input v-model="form.limitYear" placeholder="请输入保存年限" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="所在盒号" prop="boxNo">
              <el-input v-model="form.boxNo" placeholder="请输入所在盒号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所在柜号" prop="cupboardNo">
              <el-input v-model="form.cupboardNo" placeholder="请输入所在柜号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="center">
          案卷目录配置信息(
          <span style="color:red">选填</span>)
        </el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAddRecordArchiveset"
            >添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="handleDeleteRecordArchiveset"
            >删除</el-button>
          </el-col>
        </el-row>
        <el-table
          :data="recordArchivesetList"
          :row-class-name="rowRecordArchivesetIndex"
          @selection-change="handleRecordArchivesetSelectionChange"
          ref="recordArchiveset"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50" />
          <el-table-column label="目录名称" prop="catalogue">
            <template slot-scope="scope">
              <el-input v-model="scope.row.catalogue" placeholder="请输入目录名称" />
            </template>
          </el-table-column>
          <!-- <el-table-column label="资源类型" prop="fileType">
            <template slot-scope="scope">
              <el-select v-model="scope.row.fileType" @change="fileTypeChange" placeholder="请选择">
                <el-option
                  v-for="item in fileTypeOptions"
                  :key="item.key"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column> -->
          <el-table-column label="文件个数限制" prop="fileLimit">
            <template slot-scope="scope">
              <el-input-number
                v-model="scope.row.fileLimit"
                size="small"
                controls-position="right"
                :min="0"
                placeholder="请输入文件个数"
              />
            </template>
          </el-table-column>
          <el-table-column label="图片个数限制" prop="imgLimit">
            <template slot-scope="scope">
              <el-input-number
                v-model="scope.row.imgLimit"
                size="small"
                controls-position="right"
                :min="0"
                placeholder="请输入图片个数"
              />
            </template>
          </el-table-column>
          <el-table-column label="大小限制(MB)" prop="fileSize">
            <template slot-scope="scope">
              <el-input-number
                v-model="scope.row.fileSize"
                size="small"
                controls-position="right"
                :min="1"
                placeholder="请输入大小限制，单位MB"
              />
            </template>
          </el-table-column>
          <el-table-column label="上传提示" prop="point">
            <template slot-scope="scope">
              <el-input type="textarea" v-model="scope.row.point" placeholder="请输入上传提示" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listArchive,
  getArchive,
  delArchive,
  addArchive,
  updateArchive
} from "@/api/system/archive";
import { treeselect } from "@/api/system/type";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Archive",
  components: { Treeselect },
  data() {
    return {
      // 高级查询字段参数选项
      heightOptionArch: [
        {
          label: "案卷分类号",
          value: "archiveClassifiNum"
        },
        {
          label: "档号",
          value: "archiveFileNum"
        },
        {
          label: "档案馆代号",
          value: "archiveCode"
        },
        {
          label: "案卷题名",
          value: "caseFileTitle"
        },
        {
          label: "微缩号",
          value: "miniatureNum"
        },
        {
          label: "项目代号",
          value: "projectCode"
        },
        {
          label: "责任者",
          value: "personLiable"
        },
        {
          label: "总登记号",
          value: "registNum"
        },
        {
          label: "存放位置",
          value: "storagePosition"
        },
        {
          label: "编制单位",
          value: "organization"
        },
        {
          label: "总卷数",
          value: "totalRolls"
        },
        {
          label: "当前卷",
          value: "currentRolls"
        },
        {
          label: "密级",
          value: "secretLevel"
        },
        {
          label: "移交单位",
          value: "objStr2"
        }
      ],
      // 类别名称
      typeName: undefined,
      // 档案分类表格数据
      typeOptions: undefined,
      // 查询可用状态类别参数
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 树形菜单参数
      queryType: {
        status: "0"
      },
      // 文件类型
      fileTypeOptions: [
        {
          key: 0,
          label: "jpg,jpeg,png",
          value: "0"
        },
        {
          key: 1,
          label: "doc,docx,pdf,xls,xlsx",
          value: "1"
        }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedRecordArchiveset: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 案卷信息表格数据
      archiveList: [],
      // 案卷目录配置表格数据
      recordArchivesetList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        archiveNo: null,
        typeId: null,
        archiveName: null,
        subName: null,
        dutyName: null,
        limitYear: null,
        boxNo: null,
        cupboardNo: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        typeId: [
          { required: true, message: "类别不能为空", trigger: "change" }
        ],
        archiveName: [
          { required: true, message: "题名不能为空", trigger: "blur" }
        ]
      }
    };
  },
  watch: {
    // 根据名称筛选类别树
    typeName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.typeId = data.id;
      this.handleQuery();
    },
    /** 获取树形菜单结构数据 */
    getTreeselect() {
      treeselect(this.queryType).then(response => {
        this.typeOptions = response.data;
      });
    },
    fileTypeChange(arr) {
      console.log(arr);
    },
    /** 查询案卷信息列表 */
    getList() {
      this.loading = true;
      listArchive(this.queryParams).then(response => {
        this.archiveList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        archiveId: null,
        archiveNo: null,
        typeId: null,
        archiveName: null,
        subName: null,
        dutyName: null,
        limitYear: null,
        boxNo: null,
        cupboardNo: null
      };
      this.recordArchivesetList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.archiveId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加案卷信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const archiveId = row.archiveId || this.ids;
      getArchive(archiveId).then(response => {
        this.form = response.data;
        this.recordArchivesetList = response.data.recordArchivesetList;
        this.open = true;
        this.title = "修改案卷信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.recordArchivesetList = this.recordArchivesetList;
          if (this.form.archiveId != null) {
            updateArchive(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArchive(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const archiveIds = row.archiveId || this.ids;
      this.$modal
        .confirm('是否确认删除案卷信息编号为"' + archiveIds + '"的数据项？')
        .then(function() {
          return delArchive(archiveIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 案卷目录配置序号 */
    rowRecordArchivesetIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 案卷目录配置添加按钮操作 */
    handleAddRecordArchiveset() {
      let obj = {};
      obj.catalogue = "";
      obj.fileLimit = "";
      obj.imgLimit = "";
      obj.fileType = "";
      obj.fileSize = "";
      obj.point = "";
      this.recordArchivesetList.push(obj);
    },
    /** 案卷目录配置删除按钮操作 */
    handleDeleteRecordArchiveset() {
      if (this.checkedRecordArchiveset.length == 0) {
        this.$modal.msgError("请先选择要删除的案卷目录配置数据");
      } else {
        const recordArchivesetList = this.recordArchivesetList;
        const checkedRecordArchiveset = this.checkedRecordArchiveset;
        this.recordArchivesetList = recordArchivesetList.filter(function(item) {
          return checkedRecordArchiveset.indexOf(item.index) == -1;
        });
      }
    },
    /** 复选框选中数据 */
    handleRecordArchivesetSelectionChange(selection) {
      this.checkedRecordArchiveset = selection.map(item => item.index);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/archive/export",
        {
          ...this.queryParams
        },
        `archive_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
